package com.ruoyi.hr.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.hr.domain.HrUserCv;
import com.ruoyi.hr.mapper.HrUserMapper;
import com.ruoyi.hr.domain.HrUser;
import com.ruoyi.hr.service.IHrUserService;

/**
 * 用户-附表Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-02-28
 */
@Service
public class HrUserServiceImpl implements IHrUserService 
{
    @Autowired
    private HrUserMapper hrUserMapper;

    /**
     * 查询用户-附表
     * 
     * @param userUuid 用户-附表主键
     * @return 用户-附表
     */
    @Override
    public HrUser selectHrUserByUserUuid(String userUuid)
    {
        return hrUserMapper.selectHrUserByUserUuid(userUuid);
    }

    /**
     * 查询用户-附表列表
     * 
     * @param hrUser 用户-附表
     * @return 用户-附表
     */
    @Override
    public List<HrUser> selectHrUserList(HrUser hrUser)
    {
        return hrUserMapper.selectHrUserList(hrUser);
    }

    /**
     * 新增用户-附表
     * 
     * @param hrUser 用户-附表
     * @return 结果
     */
    @Transactional
    @Override
    public int insertHrUser(HrUser hrUser)
    {
        int rows = hrUserMapper.insertHrUser(hrUser);
        insertHrUserCv(hrUser);
        return rows;
    }

    /**
     * 修改用户-附表
     * 
     * @param hrUser 用户-附表
     * @return 结果
     */
    @Transactional
    @Override
    public int updateHrUser(HrUser hrUser)
    {
        hrUserMapper.deleteHrUserCvByFkHrUserUuid(hrUser.getUserUuid());
        insertHrUserCv(hrUser);
        return hrUserMapper.updateHrUser(hrUser);
    }

    /**
     * 批量删除用户-附表
     * 
     * @param userUuids 需要删除的用户-附表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHrUserByUserUuids(String[] userUuids)
    {
        hrUserMapper.deleteHrUserCvByFkHrUserUuids(userUuids);
        return hrUserMapper.deleteHrUserByUserUuids(userUuids);
    }

    /**
     * 删除用户-附表信息
     * 
     * @param userUuid 用户-附表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHrUserByUserUuid(String userUuid)
    {
        hrUserMapper.deleteHrUserCvByFkHrUserUuid(userUuid);
        return hrUserMapper.deleteHrUserByUserUuid(userUuid);
    }

    /**
     * 新增用户信息-履历信息
     * 
     * @param hrUser 用户-附表对象
     */
    public void insertHrUserCv(HrUser hrUser)
    {
        List<HrUserCv> hrUserCvList = hrUser.getHrUserCvList();
        String userUuid = hrUser.getUserUuid();
        if (StringUtils.isNotNull(hrUserCvList))
        {
            List<HrUserCv> list = new ArrayList<HrUserCv>();
            for (HrUserCv hrUserCv : hrUserCvList)
            {
                hrUserCv.setFkHrUserUuid(userUuid);
                list.add(hrUserCv);
            }
            if (list.size() > 0)
            {
                hrUserMapper.batchHrUserCv(list);
            }
        }
    }
}
